<template>
  <div class="app-container">
    <!-- 搜索 -->
    <el-card class="search-card">
      <el-form
        :model="queryParams"
        ref="queryParams"
        size="small"
        class="formBox"
        label-width="88px"
      >
        <el-row :gutter="0">
          <el-col :span="5">
            <el-form-item label="校区名称" prop="campusId">
              <el-select
                v-model="queryParams.campusId"
                placeholder="请选择校区名称"
                @change="listGradeFun"
                style="width: 100%"
              >
                <el-option
                  v-for="(item, index) in campusList"
                  :key="index"
                  :value="item.id"
                  :label="item.campusName"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="年级名称" prop="gradeId">
              <el-select
                v-model="queryParams.gradeId"
                placeholder="请选择年级名称"
                style="width: 100%"
              >
                <el-option
                  v-for="(item, index) in gradeList"
                  :key="index"
                  :value="item.id"
                  :label="item.gradeName"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="班级名称" prop="className">
              <el-input
                v-model="queryParams.className"
                placeholder="请输入班级名称"
                clearable
                maxlength="50"
                show-word-limit
              />
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="启用状态" prop="enabled">
              <el-select
                v-model="queryParams.enabled"
                placeholder="请选择启用状态"
                clearable
              >
                <el-option
                  v-for="dict in dict.type.sys_open_status"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item>
              <div class="dis aic">
                <div class="flex_1"></div>
                <el-button
                  type="primary"
                  icon="el-icon-search"
                  size="mini"
                  @click="handleQuery"
                >
                  搜索
                </el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">
                  重置
                </el-button>
              </div>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
    <!-- 按钮 -->
    <el-card class="search-card">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['campus:class:add']"
            >新增</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['campus:class:remove']"
            >删除</el-button
          >
        </el-col>
        <right-toolbar
          :showSearch.sync="showSearch"
          @queryTable="getList"
        ></right-toolbar>
      </el-row>
    </el-card>
    <!-- 列表 -->
    <el-card class="table-card">
      <el-table
        v-loading="loading"
        :data="classManageList"
        @selection-change="handleSelectionChange"
        stripe
        border
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="校区名称" align="center" prop="campusName" />
        <el-table-column label="年级名称" align="center" prop="gradeName" />
        <el-table-column label="班级名称" align="center" prop="className" />
        <el-table-column label="入学年份" align="center" prop="admissionYear" />
        <el-table-column label="排序" align="center" prop="sortOrder" />
        <el-table-column label="启用状态" align="center" prop="enabled">
          <template slot-scope="scope">
            <el-tag type="success" v-if="scope.row.enabled == 1">启用</el-tag>
            <el-tag type="danger" v-if="scope.row.enabled == 0">停用</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="创建人" align="center" prop="createdBy" />
        <el-table-column label="创建时间" align="center" prop="createTime" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime, "{y}-{m}-{d} {h}:{i}:{s}") }}</span>
          </template>
        </el-table-column>

        <el-table-column
          label="备注"
          align="center"
          prop="remarks"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-button
              type="text"
              size="mini"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['campus:class:edit']"
            >
              修改
            </el-button>
            <el-button
              type="text"
              size="mini"
              @click="handleDelete(scope.row)"
              v-hasPermi="['campus:class:remove']"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>
    <!-- 添加或修改班级管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="50%" append-to-body>
      <el-form ref="form" :model="form" label-width="180px">
        <el-row>
          <el-col :span="20">
            <el-form-item label="校区名称" prop="campusId" class="is-required">
              <el-select
                v-model="form.campusId"
                placeholder="请选择校区名称"
                style="width: 100%"
                @change="listGradeFun"
              >
                <el-option
                  v-for="(item, index) in campusList"
                  :key="index"
                  :value="item.id"
                  :label="item.campusName"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="20">
            <el-form-item label="年级名称" prop="gradeId" class="is-required">
              <el-select
                v-model="form.gradeId"
                placeholder="请输入年级名称"
                style="width: 100%"
              >
                <el-option
                  v-for="(item, index) in gradeList"
                  :key="index"
                  :value="item.id"
                  :label="item.gradeName"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="20">
            <el-form-item label="班级名称" prop="className" class="is-required">
              <el-input
                v-model="form.className"
                placeholder="请输入班级名称"
                maxlength="50"
                show-word-limit
              />
            </el-form-item>
          </el-col>
          <el-col :span="20">
            <el-form-item label="入学年份" prop="admissionYear" class="is-required">
              <el-date-picker
                v-model="form.admissionYear"
                type="year"
                placeholder="选择入学年份"
                format="yyyy"
                value-format="yyyy"
                style="width: 100%"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="20">
            <el-form-item label="排序" prop="sortOrder">
              <el-input
                v-model="form.sortOrder"
                placeholder="请输入排序"
                maxlength="4"
                @blur="itemPriceBlur"
                show-word-limit
              />
            </el-form-item>
          </el-col>
          <el-col :span="20">
            <el-form-item label="启用状态" prop="enabled" class="is-required">
              <el-radio-group v-model="form.enabled">
                <el-radio
                  v-for="dict in dict.type.sys_open_status"
                  :key="dict.value"
                  :label="parseInt(dict.value)"
                >
                  {{ dict.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="20">
            <el-form-item label="备注" prop="remarks">
              <el-input
                v-model="form.remarks"
                type="textarea"
                maxlength="200"
                show-word-limit
                placeholder="请输入内容"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listClassManage,
  getClassManage,
  delClassManage,
  addClassManage,
  updateClassManage,
} from "@/api/campus/classManage";
import { listGrade } from "@/api/campus/grade";
import { getCampusLists } from "@/api/campus/all";

export default {
  name: "class",
  dicts: ["sys_open_status"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 班级管理表格数据
      classManageList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        schoolName: null,
        gradeName: null,
        className: null,
        admissionYear: null,
        enabled: null,
        createdBy: null,
        remarks: null,
      },
      // 表单参数
      form: {},
      gradeList: [],
      campusList: [],
    };
  },
  created() {
    this.getList();
    this.getCampusListsFun();
    this.listGradeFun();
  },
  
  activated() {
    this.$forceUpdate();
    this.getList();
  },
  methods: {
    getCampusListsFun() {
      getCampusLists({ schoolId: null }).then((res) => {
        if (res.code == 200) {
          this.campusList = res.data;
        }
      });
    },
    /** 查询班级管理列表 */
    getList() {
      this.loading = true;
      listClassManage(this.queryParams).then((response) => {
        this.classManageList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /**
     * 获取年级列表
     *
     * @param options 选项对象，包含campusId
     * @param status 状态，默认为true
     */
    listGradeFun(options, status) {
      if (!status) {
        this.$set(this.form, "gradeId", null);
        this.gradeList = [];
      }
      listGrade({ pageNum: 1, pageSize: 200, campusId: options }).then((res) => {
        if (res.code == 200) {
          this.gradeList = res.rows;
        }
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单提交
    itemPriceBlur() {
      var sortOrder = this.form.sortOrder;
      this.form.sortOrder = sortOrder.replace(/[^\d]/g, "");
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        schoolName: null,
        gradeName: null,
        className: null,
        admissionYear: null,
        sortOrder: null,
        enabled: null,
        createTime: null,
        createdBy: null,
        remarks: null,
        schoolId: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryParams");
      this.gradeList = [];
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.form.enabled = 1;
      this.title = "添加班级管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getClassManage(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改班级管理";
        this.listGradeFun(response.data.campusId,true);
      });
    },
    /** 提交按钮 */
    submitForm() {
      var that = this;
      if (!this.form.campusId) {
        return that.$modal.msgError("请选择校区名称");
      }
      if (!this.form.gradeId) {
        return that.$modal.msgError("请选择年级名称");
      }
      if (!this.form.className) {
        return that.$modal.msgError("请输入班级名称");
      }
      if (!this.form.admissionYear) {
        return that.$modal.msgError("选择入学年份");
      }

      this.form.enabled = this.form.enabled + "";
      if (!this.form.enabled) {
        return that.$modal.msgError("请选择启用状态");
      }
      this.$refs["form"].validate((valid) => {
        if (this.form.id != null) {
          updateClassManage(this.form).then((response) => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          });
        } else {
          addClassManage(this.form).then((response) => {
            this.$modal.msgSuccess("新增成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm("是否确认删除班级的数据项？")
        .then(function () {
          return delClassManage(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "campus/classManage/export",
        {
          ...this.queryParams,
        },
        "班级" + `_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>

<style>
.formBox >>> .el-form-item--small.el-form-item {
  margin-bottom: 0px !important;
}

.formBox >>> .el-form-item__label {
  font-size: 12px !important;
  padding: 0 !important;
}
</style>
